package com.you.learning.springcloud.order.service.impl;

import com.you.learning.springcloud.order.dao.OrderDao;
import com.you.learning.springcloud.order.entity.Order;
import com.you.learning.springcloud.order.service.IOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    @Qualifier("rabbitTemplate")
    RabbitTemplate rabbitTemplate;

    @Autowired
    @Qualifier("rabbitTemplateWithAck")
    RabbitTemplate rabbitTemplateWithAck;

    @Override
    public int saveOrder(Order order) {
        // 创建数据库订单
        int rows = orderDao.insertOrder(order);
        // 将订单放入mq队列，用于判断是否超时支付
        String exchangeName = "springBootTopicExchange1";
        String routeKey = "springBootTopicRouteKey.test";
        String msg = "orderNo=" + order.getOrderNo() + ", orderName=" + order.getOrderName();
        Message message = new Message(msg.getBytes(), new MessageProperties());
        rabbitTemplate.send(exchangeName, routeKey, message);
        return rows;
    }

    @Override
    public int saveOrder(Order order, String exchangeName,String routeKey) {
        // 创建数据库订单
        int rows = orderDao.insertOrder(order);
        // 将订单放入mq队列，用于判断是否超时支付
        //String routeKey = "springBootTopicRouteKey.test";
        String msg = "orderNo=" + order.getOrderNo() + ", orderName=" + order.getOrderName();
        Message message = new Message(msg.getBytes(), new MessageProperties());
        rabbitTemplateWithAck.send(exchangeName, routeKey, message);
        return rows;
    }
}
